package controller;

import com.google.gson.Gson;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.Admin;
import service.AdminService;
import utils.util;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @Resource
    private HttpServletResponse response;


    @CrossOrigin
    @RequestMapping("/adminLogin")
    public void adminLogin(int adminid, String pwd, HttpServletResponse response) throws NoSuchAlgorithmException {
        System.out.println(adminid);
        System.out.println("传过来得知"+pwd);
       String password= util.UseMD5(pwd);
        Admin admin = new Admin();
        admin = adminService.adminLogin(adminid);
        System.out.println("admin的值"+admin.getPassword());
        if (password.equals(admin.getPassword())) {
            try {
                String strAdmin=new Gson().toJson(admin);
                response.setContentType("charset=UTF-8");
                response.getWriter().write(strAdmin);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.setContentType("charset=UTF-8");
                response.getWriter().write("error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @CrossOrigin
    @RequestMapping("/adminRegister")
    public void adminRegister(int adminid, String pwd, String phone) throws IOException, NoSuchAlgorithmException {
        System.out.println(adminid);
        System.out.println(phone);
        System.out.println(pwd);
        Admin admin2 = adminService.adminLogin(adminid);
        if (admin2 != null) {
            response.setContentType("charset=UTF-8");
            response.getWriter().write("该用户已注册");
        } else {
            Admin admin3 = new Admin();
            admin3.setAdminId(adminid);
            admin3.setPassword(util.UseMD5(pwd));
            admin3.setPhone(phone);
            admin3.setType("管理员");
            int i = adminService.insert(admin3);
            if (i > 0) {
                response.setContentType("charset=UTF-8");
                response.getWriter().write("success");
            } else {
                response.setContentType("charset=UTF-8");
                response.getWriter().write("error");
            }
        }

    }


    @CrossOrigin
    @RequestMapping("/selectWebAdmin")
    public void selectWebAdmin(HttpServletResponse response){
        System.out.println("页面数据查看被调用");
        List<Admin> list=adminService.selectWebAdmin();
        String JsonStr=new Gson().toJson(list);
        response.setCharacterEncoding("utf-8");
        System.out.println(JsonStr);
        try {
            response.getWriter().write(JsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CrossOrigin
    @RequestMapping("/delete")
    public void delete(int id){
        System.out.println("已删除");
        System.out.println("传过来的值："+id);
        int a=adminService.delete(id);
        System.out.println("返回值"+a);

    }
    @CrossOrigin
    @RequestMapping("/adminUpdate")
    public void adminUpdate(int adminid,String name,String phone,String adminEmail,String remake, HttpServletResponse response) throws IOException {
        System.out.println(adminid);
        Admin admin = new Admin();
        admin.setAdminId(adminid);
        admin.setName(name);
        admin.setPhone(phone);
        admin.setEmail(adminEmail);
        admin.setRemake(remake);
        int i=adminService.updateAdmin(admin);
        if(i>0){
            String strAdmin=new Gson().toJson(admin);
             response.setContentType("charset=UTF-8");
            response.getWriter().write(strAdmin);
        }else {
            response.setContentType("charset=UTF-8");
            response.getWriter().write("error");
        }

    }


    @CrossOrigin
    @RequestMapping("/adminUpPwd")
    public void adminUpPwd(int adminid,String password, HttpServletResponse response) throws IOException, NoSuchAlgorithmException {
        System.out.println(adminid);
        String password1= util.UseMD5(password);
        Admin admin = new Admin();
        admin.setAdminId(adminid);
        admin.setPassword(password1);
        int i=adminService.updateAdminpwd(admin);
        if(i>0){
            response.setContentType("charset=UTF-8");
            response.getWriter().write("success");
        }else {
            response.setContentType("charset=UTF-8");
            response.getWriter().write("error");
        }

    }

}
